package cn.tedu.eb_film.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 状态JSON
 *
 * @author lenner ronte
 * @since 2023/2/22 15:50
 */
@Data
//链式写法
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "响应")
public class  JsonResult<T> implements Serializable {
    @ApiModelProperty(value = "请求状态")
    private Integer state;
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "响应消息")
    private String message;
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "响应数据")
    private T data;

    public static JsonResult<Void> ok() {
        return JsonResult.ok(null);
    }

    public static <T> JsonResult<T> ok(T data) {
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.state = ServiceCode.OK.getValue();
        jsonResult.data = data;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceCode state, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.state = state.getValue();
        jsonResult.message = message;
        return jsonResult;
    }

}
